package cn.jsj.invite.controller;

import cn.jsj.invite.interceptor.swich.PassToken;
import cn.jsj.invite.pojo.UserEnt;
import cn.jsj.invite.result.Result;
import cn.jsj.invite.service.EmailService;
import cn.jsj.invite.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

import javax.servlet.http.HttpServletRequest;

@Controller
@ResponseBody
@CrossOrigin
@RequestMapping(value = "/user")
public class UserController {

    private final UserService userService;

    private final EmailService emailService;

    @Autowired
    public UserController(UserService userService, EmailService emailService) {
        this.userService = userService;
        this.emailService = emailService;
    }

    /**
     * 登录接口
     */
    @PassToken
    @PostMapping(value = "/login")
    public Result<UserEnt> login(@RequestBody UserEnt userEnt) {
        UserEnt userInfo = userService.userLogin(userEnt.getAccount(), userEnt.getUserIdentity(), userEnt.getPassword());
        return Result.success(userInfo);
    }

    /**
     * 生成6位验证码并发送到目标邮箱
     */
    @PassToken
    @GetMapping(value = "/code", params = {"email"})
    public Result<Object> check(@RequestParam("email") String email, HttpServletRequest request) {
        emailService.sendVerificationCode(120000, email, request);
        return Result.success();
    }


}
